package com.tong.embeddedvr.controller;

import com.tong.embeddedvr.dto.CollectionDTO;
import com.tong.embeddedvr.dto.TableReqDTO;
import com.tong.embeddedvr.dto.TableRspDTO;
import com.tong.embeddedvr.dto.UserInputDTO;
import com.tong.embeddedvr.service.impl.CollectionServiceImpl;
import com.tong.embeddedvr.utils.CommonUtil;
import org.apache.ibatis.annotations.ResultMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * TODO
 *
 * @author gcl
 * @version 1.0
 * @Description
 * @date 2021/1/1 11:05
 */
@CrossOrigin
@RestController
@RequestMapping("/collection")
public class CollectionController {

    @Autowired
    private CollectionServiceImpl collectionService;

    @RequestMapping("/showtable")
    public String getTable(TableReqDTO tableReqDTO,UserInputDTO userInputDTO){
        TableRspDTO tableRspDTO = null;
        String res="请联系管理员";
        try {
            tableRspDTO = collectionService.showTable(tableReqDTO, userInputDTO);
            res = CommonUtil.ajaxReturn(tableRspDTO);
        } catch (Exception e) {
            e.printStackTrace();
            res = CommonUtil.ajaxReturn("更新失败,请稍后重试!");
        }
        return res;
    }

    @RequestMapping("/delete")
    public String delete(CollectionDTO collectionDTO){
        Integer num = collectionService.delete(collectionDTO);
        String res = "请联系管理员";
        if(num==1){
            res = CommonUtil.ajaxReturn("更新成功");
        }else{
            res = CommonUtil.ajaxReturn(400,"更新失败,请稍后重试!");
        }
        return res;
    }

    @RequestMapping("/add")
    public String add(CollectionDTO collectionDTO){
        Integer num = collectionService.add(collectionDTO);
        String res = "请联系管理员";
        if(num==1){
            res = CommonUtil.ajaxReturn("更新成功!");
        }else{
            res = CommonUtil.ajaxReturn(400,"更新失败,请稍后重试");
        }
        return res;
    }

    @RequestMapping("/showcollection")
    public String showCollection(CollectionDTO collectionDTO){
        String res = "请联系管理员";
        try{
            CollectionDTO collectionInformation = collectionService.showCollectionInformation(collectionDTO);
            res = CommonUtil.ajaxReturn(collectionInformation);
        }catch (Exception e){
            e.printStackTrace();
            res = CommonUtil.ajaxReturn(400,"请求失败,请联系管理员!");
        }
        return res;
    }

    @RequestMapping("/getunpassedcollection")
    public String showUnpassedCollection(UserInputDTO userInputDTO){
        String res = "请联系管理员";
        try {
            List<CollectionDTO> collectionDTOList = collectionService.showUnpassedCollection(userInputDTO);
            res = CommonUtil.ajaxReturn(collectionDTOList);
        } catch (Exception e) {
            e.printStackTrace();
            res = CommonUtil.ajaxReturn(400,"请求错误,请稍后重试");
        }
        return res;
    }
}
